﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.Charting;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCA.UI.Utils;
using Baosight.ZNH.ZMCommon;

namespace Baosight.ZNH.ZMCA.UI
{
    public partial class ZMCA24 : EFViewCmes
    {
        public ZMCA24()
        {
            InitializeComponent();
            InitControls();
        }

        private void InitControls()
        {
            List<EIRow> lstProblemStatus = new List<EIRow>();

            EIRow r = new EIRow();
            //r.Add("Name", "草拟");
            //r.Add("Value", "0");
            //lstProblemStatus.Add(r);

            //r = new EIRow();
            r.Add("Name", "未处理");
            r.Add("Value", "1");
            lstProblemStatus.Add(r);

            r = new EIRow();
            r.Add("Name", "未反馈");
            r.Add("Value", "2");
            lstProblemStatus.Add(r);

            r = new EIRow();
            r.Add("Name", "未核查");
            r.Add("Value", "3");
            lstProblemStatus.Add(r);

            r = new EIRow();
            r.Add("Name", "关闭");
            r.Add("Value", "4");
            lstProblemStatus.Add(r);

            //r = new EIRow();
            //r.Add("Name", "作废");
            //r.Add("Value", "5");
            //lstProblemStatus.Add(r);

            ddProblemStatus.BindItemsToGrid(lstProblemStatus, "Name");

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA24");
            eiInfo.Set("method", "queryProblemType");

            EIBlock conBlock = eiInfo.AddBlock("inqu_status");
            //fdDate代表开始日期，displayName代表截止日期
            Common.AddEIColumnsToEIBlock(conBlock, new string[] { "fdNo"});
            EIRow conRow = new EIRow();
            conRow.Add("fdNo", "cmes.cp.problemType");
            conBlock.AddRow(conRow);

            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, queryProblemTypeCompleted);

            //趋势图

            ProblemTypeChart.DefaultView.ChartArea.ItemClick += new EventHandler<ChartItemClickEventArgs>(ChartArea_ItemClick);

            Common.ClearChart(ProblemTypeChart);
            Common.ClearChart(PieChart);
        }

        private void InitPieChart()
        {
            Common.ClearChart(PieChart);
            PieChart.DefaultView.ChartTitle.Content = "单位分布";
            PieChart.DefaultView.ChartLegend.Header = "";
            PieChart.DefaultView.ChartLegendPosition = Telerik.Windows.Controls.Dock.Bottom;
            SeriesMapping smPie = new SeriesMapping();
            PieSeriesDefinition totalPie = new PieSeriesDefinition();
            smPie.SeriesDefinition = totalPie;
            //sm2.LegendLabel = "单位";
            smPie.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            smPie.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
            smPie.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));

            totalPie.InteractivitySettings.HoverScope = InteractivityScope.Item;
            totalPie.InteractivitySettings.SelectionScope = InteractivityScope.Item;
            totalPie.InteractivitySettings.SelectionMode = ChartSelectionMode.Single;
            totalPie.LegendDisplayMode = LegendDisplayMode.DataPointLabel;

            PieChart.SeriesMappings.Add(smPie);
        }

        private void InitTrendChart()
        {
            ProblemTypeChart.SeriesMappings.Clear();
            ProblemTypeChart.DefaultView.ChartTitle.Content = "问题类型分布图";
            ProblemTypeChart.DefaultSeriesDefinition.ShowItemToolTips = true;
            ProblemTypeChart.DefaultSeriesDefinition.LegendDisplayMode = LegendDisplayMode.None;
            ProblemTypeChart.DefaultView.ChartLegendPosition = Telerik.Windows.Controls.Dock.Bottom;
            ProblemTypeChart.DefaultView.ChartLegend.Header = "";

            SeriesMapping smBar = new SeriesMapping();
            BarSeriesDefinition totalBar = new BarSeriesDefinition();
            smBar.SeriesDefinition = totalBar;
            smBar.SeriesDefinition.ShowItemLabels = false;
            smBar.SeriesDefinition.ShowItemToolTips = true;
            smBar.SeriesDefinition.ShowZeroValuesLabels = true;
            smBar.ItemMappings.Add(new ItemMapping("XCategory", DataPointMember.XCategory));
            smBar.ItemMappings.Add(new ItemMapping("YValue", DataPointMember.YValue));
            smBar.ItemMappings.Add(new ItemMapping("LegendLabel", DataPointMember.LegendLabel));
            smBar.SeriesDefinition.InteractivitySettings.HoverScope = InteractivityScope.Item;
            smBar.SeriesDefinition.InteractivitySettings.SelectionScope = InteractivityScope.Item;
            smBar.SeriesDefinition.InteractivitySettings.SelectionMode = ChartSelectionMode.Single;

            totalBar.LegendDisplayMode = LegendDisplayMode.DataPointLabel;
            ProblemTypeChart.SeriesMappings.Add(smBar);
        }

        private void queryProblemTypeCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eiInfo = e.EIInfo;
            EIBlock resultBlock = eiInfo.GetBlock("result");
            ddProblemType.BindItemsToGrid(resultBlock.Rows, "fdDetailsName");
        }

        public void OnQueryButtonClick(object sender, RoutedEventArgs e)
        {
            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA24");
            eiInfo.Set("method", "queryAll");


            IList<CheckBox> selectedProbelemStatus = ddProblemStatus.LstCheckedItems;
            string problemStatus = "''";
            foreach (var item in selectedProbelemStatus)
            {
                problemStatus = problemStatus + ",'" + (item.DataContext as EIRow)["Value"].ToString() + "'";
            }

            IList<CheckBox> selectedProbelemType = ddProblemType.LstCheckedItems;
            string problemType = "''";
            foreach (var item in selectedProbelemType)
            {
                problemType = problemType + ",'" + (item.DataContext as EIRow)["fdKeyId"].ToString()+"'";
            }

            EIBlock conBlock = eiInfo.AddBlock("inqu_status");
            Common.AddEIColumnsToEIBlock(conBlock, new string[] { "fdToDate", "fdFromDate", "fdProblemTypeId", "fdProblemStatus" });
            EIRow conRow = new EIRow();
            conRow.Add("fdFromDate", datePicker.FromDate.ToString(Common.DateTimeFormatDate));
            conRow.Add("fdToDate", datePicker.ToDate.ToString(Common.DateTimeFormatDate));
            conRow.Add("fdProblemStatus", problemStatus);
            conRow.Add("fdProblemTypeId", problemType);

            conBlock.AddRow(conRow);

            ProblemTypeChart.Tag = conRow;
            Common.ClearChart(ProblemTypeChart);
            Common.ClearChart(PieChart);
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, queryAllCompleted);
            (sender as Button).IsEnabled = false;
            TempButton = sender as Button;
        }

        private void queryAllCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eiInfo = e.EIInfo;
            EIBlock resultBlock = eiInfo.GetBlock("result");
            if (resultBlock.Rows.Count <= 0)
            {
                Common.Alert("没有找到数据");
                base.TempButtonOpen(true);
                return;
            }

            InitTrendChart();
            ProblemTypeChart.ItemsSource = null;
            List<DataPoint> totalList = new List<DataPoint>();

            foreach (var item in resultBlock.Rows)
            {
                string date = item["fdKeyId"].ToString();
                if (date.Trim() == string.Empty)
                {
                    continue;
                }

                DataPoint dp = new DataPoint();
                dp.YValue = Common.GetIntValue(item["fdKeyId"]);
                dp.XCategory = Common.GetStringValue(item["fdProblemTypeName"]);
                dp.LegendLabel = Common.GetStringValue(item["fdProblemTypeName"]);
                dp.DataItem = Common.GetStringValue(item["fdProblemTypeId"]);

                totalList.Add(dp);
            }

            ProblemTypeChart.ItemsSource = totalList;
            
            base.TempButtonOpen(true);
        }


        void ChartArea_ItemClick(object sender, ChartItemClickEventArgs e)
        {
            //selectedData格式为yyyy-MM
            DataPoint selectedData = e.DataPoint.DataItem as DataPoint;

            EIInfo eiInfo = new EIInfo();
            eiInfo.Set("service", "ZMCA24");
            eiInfo.Set("method", "queryByProblemType");

            EIBlock conBlock = eiInfo.AddBlock("inqu_status");
            Common.AddEIColumnsToEIBlock(conBlock, new string[] { "fdToDate", "fdFromDate", "fdProblemTypeId", "fdProblemStatus" });
            EIRow conRow = ProblemTypeChart.Tag as EIRow;
            conRow.Add("fdProblemTypeId", selectedData.DataItem.ToString());
            conBlock.AddRow(conRow);

            PieChart.Tag = selectedData.YValue;
            HttpServiceAgent agent = new HttpServiceAgent();
            agent.CallService(eiInfo, queryByPieCompleted);
        }

        private void queryByPieCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            InitPieChart();
            EIInfo eiInfo = e.EIInfo;
            EIBlock resultBlock = eiInfo.GetBlock("result");

            //List<List<DataPoint>> itemsSource = new List<List<DataPoint>>();
            List<DataPoint> totalList = new List<DataPoint>();

            int departmentCount = 1;
            int totalForTop5Department = 0;
            foreach (var item in resultBlock.Rows)
            {
                string date = item["fdBusinessDepartmentId"].ToString();
                if (date.Trim() == string.Empty)
                {
                    continue;
                }

                DataPoint dp = new DataPoint();
                dp.YValue = Common.GetIntValue(item["fdKeyId"]);
                totalForTop5Department += Common.GetIntValue(item["fdKeyId"]);
                dp.XCategory = Common.GetStringValue(item["fdDepartmentName"]);
                dp.LegendLabel = Common.GetStringValue(item["fdDepartmentName"]);

                totalList.Add(dp);

                departmentCount++;
                if (departmentCount > 5)
                {
                    break;
                }
            }
            if (resultBlock.Rows.Count > 5)
            {
                DataPoint dp = new DataPoint();
                dp.YValue = Common.GetIntValue(PieChart.Tag) - totalForTop5Department;
                dp.XCategory = "其他";
                dp.LegendLabel = "其他";
                totalList.Add(dp);
            }

            PieChart.ItemsSource = null;
            PieChart.ItemsSource = totalList;
        }
    }
}
